package com.xiaoshuidi.cloud.module.report.xxljob;

import com.xiaoshuidi.cloud.framework.tenant.core.aop.TenantIgnore;
import com.xiaoshuidi.cloud.module.report.dal.DO.report.IncomeSummaryReportDO;
import com.xiaoshuidi.cloud.module.report.service.finance.IncomeDetailReportService;
import com.xiaoshuidi.cloud.module.report.service.finance.IncomeSummaryReportService;
import com.xiaoshuidi.cloud.module.report.utils.DateUtils;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 收入汇总处理job每天凌晨6跑一次
 * 需将汇总明细的数据进行再次汇总
 */
@Component
@Slf4j
public class IncomeSummaryHandleJob {

    @Resource
    private IncomeDetailReportService incomeDetailReportService;

    @Resource
    private IncomeSummaryReportService incomeSummaryReportService;

    @XxlJob("incomeSummaryHandleJob")
    @TenantIgnore
    public void execute() {
        //获取当月所有的明细
        //获取昨天所在当月所有的明细
        Integer minusDays = 1;
        String params = XxlJobHelper.getJobParam();
        if(StringUtils.isNotBlank(params)){
            try {
                minusDays = Integer.parseInt(params);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        String currentMonth = DateUtils.getYesterdayMonthAnI(minusDays);

        List<IncomeSummaryReportDO> incomeSummaryReportDOS = incomeDetailReportService.selectDetailsInCurrentMonthGroupByApartmentId(currentMonth);

        if(CollectionUtils.isNotEmpty(incomeSummaryReportDOS)){
            List<IncomeSummaryReportDO> summaryReportDOList = incomeSummaryReportService.selectSumByCurrentMonth(currentMonth);
            if(CollectionUtils.isEmpty(summaryReportDOList)){
                incomeSummaryReportService.batchInsert(incomeSummaryReportDOS);
            }else {
                List<Long> apartmentIds = summaryReportDOList.stream().map(IncomeSummaryReportDO::getApartmentId).collect(Collectors.toList());
                List<IncomeSummaryReportDO> updateList = incomeSummaryReportDOS.stream().filter(e -> apartmentIds.contains(e.getApartmentId())).collect(Collectors.toList());
                List<IncomeSummaryReportDO> insertList = incomeSummaryReportDOS.stream().filter(e -> !apartmentIds.contains(e.getApartmentId())).collect(Collectors.toList());
                if(CollectionUtils.isNotEmpty(updateList)){
                    updateList.forEach(e->{
                        summaryReportDOList.forEach(report->{
                            if(e.getApartmentId().equals(report.getApartmentId())){
                                e.setId(report.getId());
                            }
                        });
                    });
                    incomeSummaryReportService.batchUpdate(updateList);
                }
                if(CollectionUtils.isNotEmpty(insertList)){
                    incomeSummaryReportService.batchInsert(insertList);
                }
            }
        }
    }

}